package org.infinitest.swingui;

import static org.junit.Assert.*;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import org.infinitest.EventFakeSupport;
import org.junit.Before;
import org.junit.Test;

public class WhenRenderingTreeNodes
{
  private FailureCellRenderer cellRenderer;
  private InfinitestMainFrame mainFrame;

  @Before
  public void inContext()
  {
    mainFrame = new InfinitestMainFrame();
    cellRenderer = (FailureCellRenderer) mainFrame.getTree().getCellRenderer();
  }

  @Test
  public void shouldHaveTooltipToInformUsersAboutClickFunctionality() throws Exception
  {
    assertEquals("Double-click test nodes for more info", cellRenderer.getToolTipText());
  }

  @Test
  public void shouldHaveIconToIndicatePointOfFailureNodes() throws Exception
  {
    Object node = "PointOfFailure.java:32";
    JLabel treeCell = (JLabel) cellRenderer.getTreeCellRendererComponent(mainFrame.getTree(), node, false, false, false, 0, false);
    assertEquals(expectedIcon("error"), treeCell.getIcon().toString());
  }

  @Test
  public void shouldHaveIconToIndicateTestNodes() throws Exception
  {
    Object node = EventFakeSupport.withAnyTest();
    JLabel treeCell = (JLabel) cellRenderer.getTreeCellRendererComponent(mainFrame.getTree(), node, false, false, false, 0, false);
    assertEquals(expectedIcon("failure"), treeCell.getIcon().toString());
  }

  private String expectedIcon(String iconName)
  {
    return new ImageIcon(getClass().getResource("/org/infinitest/swingui/" + iconName + ".png")).toString();
  }
}
